﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
namespace WebApplication1
{
    public class FileLogHelper
    {
        public static void WriteTxtLog(string functionName, string logStr)
        {
            string strPath; //错误文件的路径  
            DateTime dt = DateTime.Now;
            try
            {
                strPath = System.AppDomain.CurrentDomain.BaseDirectory + "ErrorLog";
                if (Directory.Exists(strPath) == false)  //工程目录下 Log目录 '目录是否存在,为true则没有此目录   
                {
                    Directory.CreateDirectory(strPath); //建立目录　Directory为目录对象     
                }
                strPath = strPath + "\\" + dt.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter FileWriter = new StreamWriter(strPath, true); //创建日志文件    
                FileWriter.WriteLine("Time: " + dt.ToString("HH:mm:ss") + " "+ functionName+": " + logStr);
                FileWriter.Close(); //关闭StreamWriter对象
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static void WriteTxtLog_zyd(string functionName, string logStr,string  yhm)
        {
            string strPath; //错误文件的路径  
            DateTime dt = DateTime.Now;
            try
            {
                strPath = System.AppDomain.CurrentDomain.BaseDirectory + "ErrorLog";
                if (Directory.Exists(strPath) == false)  //工程目录下 Log目录 '目录是否存在,为true则没有此目录   
                {
                    Directory.CreateDirectory(strPath); //建立目录　Directory为目录对象     
                }
                strPath = strPath + "\\" + yhm + dt.ToString("yyyy-MM-dd") + ".txt";
                StreamWriter FileWriter = new StreamWriter(strPath, true); //创建日志文件    
                FileWriter.WriteLine("Time: " + dt.ToString("HH:mm:ss") + " " + functionName + ": " + logStr);
                FileWriter.Close(); //关闭StreamWriter对象
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static PropertyInfo[] GetPropertyInfoArray<T>()
        {
            PropertyInfo[] props = null;
            try
            {
                Type type = typeof(T);
                object obj = Activator.CreateInstance(type);
                props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return props;
        }

        public static string GetTXml<T>(T tModel)
        {
            var propList = GetPropertyInfoArray<T>();
            string tempXml = string.Empty;
            foreach (var item in propList)
            {
                tempXml += "<" + item.Name + ">" + tModel.GetValue(item.Name) + "</" + item.Name + ">";
            }
            return tempXml;
        }
    }
}
